package cn.fl.dk.web.controller;

import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.fl.dk.web.service.ITemplateService;
import cn.fl.dk.web.vo.CalculateResult;
import cn.fl.dk.web.vo.QuestionVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 问题回答模板
 * 作者:hongyanbo
 * 时间:2018/6/4
 */
@Controller
@RequestMapping("/template")
public class TemplateController {
    private Logger log = LoggerFactory.getLogger(TemplateController.class);

    @Autowired
    private ITemplateService templateService ;
    /**
     *  to回答问题页面
     * @param templateCd 模板Cd
     * @return
     */
    @RequestMapping("toAddPage.do")
    @ResponseBody
    public Object toAddPage(String templateCd  ){
        // questionVO
        List<QuestionVO> qList =null;
        try{
            qList  = templateService.getQuestion(templateCd);
        }catch (Exception e){
            BaseLogUtils.info(log,"toAddPage",e.getMessage());
        }
        return qList;
    }

    /**
     * 保存问卷
     * @param templateId
     * @param templateName
     * @param selection json 选择了第几题的第几个选项
     * @param pkId  业务主键
     * @param tableName 业务表名
     * @param qtnType 试卷类型，0：未出险/1：已出险 试卷
     * @param status  1:暂存 2提交
     * @param deleteSelection  要删除的选项
     * @return
     */
    @RequestMapping("save.do")
    @ResponseBody
    public Object save(String templateId , String templateName , String selection,String pkId,String tableName,Integer qtnType,String status,String deleteSelection){
        // 计算评分结果
        CalculateResult result = new CalculateResult();
        result.setScore(0l);
        result.setIsMatch("1");
        if(status.equals("2")){ //暂存不计算分数
             result = templateService.calculateScore(selection, qtnType);
        }
        // 保存评分记录
        Long recId = this.templateService.saveRec(templateId, templateName, pkId, tableName, result.getScore());
        // 保存评分记录明细
        this.templateService.saveRecDtl(recId,selection);
        // 删除评分记录明细
        this.templateService.deleteRecDtl(recId,deleteSelection);
        return result;
    }

    /**
     * 修改问卷
     * @param templateId
     * @param templateName
     * @param selection json 选择了第几题的第几个选项
     * @param pkId  业务主键
     * @param tableName 业务表名
     * @param qtnType 试卷类型，0：未出险/1：已出险 试卷
     * @param status  1:暂存 2提交
     * @param deleteSelection  要删除的选项
     * @return
     */
    @RequestMapping("update.do")
    @ResponseBody
    public Object update(String templateId , String templateName , String selection,String pkId,String tableName,Integer qtnType,String status,String deleteSelection){
        // 计算评分结果
        CalculateResult result = new CalculateResult();
        result.setScore(0l);
        result.setIsMatch("1");
        if(status.equals("2")){
            result =templateService.calculateScore(selection,qtnType);
        }
        // 更新评分记录
        Long recId = this.templateService.updateRec(templateId, templateName, pkId, tableName, result.getScore());
        // 保存评分记录明细
        this.templateService.updateRecDtl(recId,selection);
        // 删除评分记录明细
        this.templateService.deleteRecDtl(recId,deleteSelection);
        return result;
    }

    /**
     *
     * @param id 业务主键
     * @param templateCd
     * @return
     */
    @RequestMapping("view.do")
    @ResponseBody
    public Object view(String id,String templateCd  ){
        BaseLogUtils.info(log,"view","请求view.do");

        List<QuestionVO> qList=null;
        try{
        // 问题选项
            qList= templateService.getQuestion(templateCd);
        // 查询答案
        templateService.assembleAnswer(qList,id);
        }catch (Exception e){
            BaseLogUtils.info(log,"view",e.getMessage());
        }

        return qList;
    }
}
